﻿// Decompiled with JetBrains decompiler
// Type: MCM.SettingsProviderMissionBehavior
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Abstractions;
using MCM.Abstractions.Base;
using System.Collections.Concurrent;
using TaleWorlds.MountAndBlade;

#nullable enable
namespace MCM
{
  public class SettingsProviderMissionBehavior : MissionBehavior
  {
    private readonly BaseSettingsProvider? _baseSettingsProvider;
    private readonly ConcurrentDictionary<System.Type, string> _cache = new ConcurrentDictionary<System.Type, string>();

    public override MissionBehaviorType BehaviorType => MissionBehaviorType.Other;

    public SettingsProviderMissionBehavior(BaseSettingsProvider? baseSettingsProvider)
    {
      this._baseSettingsProvider = baseSettingsProvider;
    }

    public TSettings? Get<TSettings>() where TSettings : BaseSettings, new()
    {
      if (!this._cache.ContainsKey(typeof (TSettings)))
        this._cache.TryAdd(typeof (TSettings), new TSettings().Id);
      return this._baseSettingsProvider?.GetSettings(this._cache[typeof (TSettings)]) as TSettings;
    }
  }
}
